﻿using System;
using System.Threading;
using PersonsServer;
using System.Net;

namespace PersonsServer_Sample
{
    static class Program
    {
        const int Port = 10011;

        static void Main()
        {
            var ip = IPAddress.Loopback;
            var serverThread = new Thread(ServerStart);
            serverThread.Start();
            Console.Write("Server starts at {0} on port {1}.\nPress Enter to stop...\n", ip, Port);
            Console.ReadLine();
            serverThread.Join();
        }

        static void ServerStart()
        {
            var server = new PServer();
            server.PersonsMessageEvent += ServerPersonsMessageEvent;
            server.StartEvent += ServerStartEvent;
            server.Start(IPAddress.Parse("127.0.0.1"), Port, 10);
        }

        static void ServerStartEvent(object sender, EventArgs e)
        {
            Console.WriteLine("Server started");
        }

        static void ServerPersonsMessageEvent(object sender, PersonMessageEventArgs e)
        {
            Console.WriteLine("\nMessage Received:\n" + 
                "Person id:  {0}\n" + 
                "First name: {1}\n" + 
                "Last Name:  {2}\n" + 
                "Age:        {3}\n" + 
                "Mobile #:   {4}\n" + 
                "Gender:     {5}\n", 
                e.Person.Id, e.Person.FirstName, e.Person.LastName, 
                e.Person.Age, e.Person.MobilePhone, e.Person.PGender);
        }
    }
}
